<template>
  <div class="custom-body" :style="borderStyle">
    <div class="custom-content">
      <div class="custom-header">
        <div class="header-input">
          <div class="header-input-span">
            <input
              v-model="form.blurry"
              placeholder="搜索您需要的内容 / Search Content"
              @keyup="queryObject"
            >&nbsp;&nbsp;&nbsp;
            <div class="header-input-icon" @click="queryObject">
              <i class="el-icon-search" />
            </div>
          </div>
        </div>
      </div>
      <el-empty
        v-if="dataList === null || dataList.length === 0"
        :image-size="100"
      />
      <div v-else class="custom-content-ul">
        <div class="content-body">
          <div
            v-for="(obj, index) in dataList"
            :key="'server-' + index"
            class="body-li"
            @click="openEntity(obj)"
          >
            <div class="li-image">
              <el-image lazy class="custom-image" :src="obj.appIcon" />
            </div>
            <div class="li-title">{{ obj.name }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getToken } from '@/utils/auth';
import {
  queryAllSystemSys,
  queryAllGuestSystemSys
} from '@/api/portal/manager/analysis';
export default {
  name: 'AppCustomApplication', // app应用中心
  props: {
    temp: {
      type: Object,
      default() {
        return {};
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      dataList: [],
      column: [],
      queryList: [],
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      form: {
        blurry: '',
        cid: null,
        page: 1,
        size: 9999
      },
      customStyle: {},
      titleStyle: {},
      bodyStyle: {}
    };
  },
  computed: {
    borderStyle() {
      if (this.temp.sfdzqp === true) {
        return {
          position: 'absolute',
          left: '0px',
          right: '0px',
          top: '0px',
          bottom: '0px'
        };
      }
      return { height: this.temp.defaultHeight };
    },
    headerStyle() {
      if (this.temp.sfzdy === true) {
        return {
          backgroundColor: this.temp.backgroundColor,
          color: this.temp.fontColor
        };
      }
      return {};
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData;
        this.titleStyle = {
          backgroundColor: newData.mrlb,
          opacity: newData.tmd
        };
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        this.query = {
          blurry: ''
        };
        if (this.showHeader) {
          const pzx = JSON.parse(newData.data.pzx);
          this.column = pzx;
        } else {
          const pzx = JSON.parse(newData.pzx);
          this.column = pzx;
        }
        if (undefined !== this.column.query && this.column.query !== null) {
          this.queryList = this.column.query;
        } else {
          this.queryList = [];
        }
        console.log(newData);
        this.form.cid = newData.data.id;
        this.initData();
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    queryObject() {
      this.initData();
    },
    openEntity(data) {
      // 打开详情页
      if (this.column.isOpenEntity) {
        // 允许打开详情页
        const params = [];
        let url = '';
        if (
          undefined !== this.column.entityParams &&
          this.column.entityParams !== null &&
          this.column.entityParams.length > 0
        ) {
          this.column.entityParams.forEach(item => {
            const obj = { code: item.code, value: data[item.value] };
            params.push(obj);
          });
        }
        if (this.column.entityOpenType === 1) {
          // 地址参数的方式打开
          url = data[this.column.entityUrl];
        }
        if (this.column.entityOpenType === 2) {
          // 地址传参查询
          if (
            undefined !== this.column.params &&
            this.column.params !== null &&
            this.column.params.length > 0
          ) {
            url = this.column.entityUrl;
            this.column.params.forEach(item => {
              url = url.replace(
                new RegExp(
                  '\\$\\{([\\s]*?)(' + item.code + '{1})([\\s]*?)([^}])*\\}'
                ),
                data[item.column]
              );
            });
          }
        }
        const object = {
          title: data.name,
          params: params,
          setting: this.temp,
          entityTemp: this.column.entityTemplateId,
          temp: this.column.entityTemp,
          openType: this.column.entityOpenType,
          url: url
        };
        this.$emit('fetch-entity', object);
      }
    },
    initData() {
      if (getToken()) {
        queryAllSystemSys(this.form)
          .then(res => {
            const { data } = res;
            if (undefined !== data.data && data.dataList !== null) {
              this.dataList = data.data;
            }
          })
          .catch(() => {
            this.dataList = [];
          });
      } else {
        queryAllGuestSystemSys(this.form)
          .then(res => {
            const { data } = res;
            if (undefined !== data.data && data.dataList !== null) {
              this.dataList = data.data;
            }
          })
          .catch(() => {
            this.dataList = [];
          });
      }
    },
    queryClick(obj) {
      this.query[obj.code] = obj.value;
      this.initData();
    }
  }
};
</script>

<style scoped>
.custom-content {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #409eff0f;
}
.header-input-span > input {
  width: 100%;
  border: none;
  outline: none;
}
.custom-header {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  background-color: #409eff0f;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
.custom-content-ul {
  margin-top: 10px;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  background-color: #409eff0f;
  /* box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04); */
}
.header-input {
  width: 100%;
  line-height: 40px;
  height: 40px;
  padding: 5px 15px;
  box-sizing: border-box;
  font-size: 13px;
}
.header-input-span {
  width: 100%;
  height: 100%;
  border-radius: 18px;
  background-color: #ffffff;
  padding: 0px 20px;
  box-sizing: border-box;
  position: relative;
  line-height: 30px;
  color: #606266;
}
.header-input-icon {
  position: absolute;
  right: 5px;
  top: 0px;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
}
.content-body {
  width: 100%;
  display: flex;
  flex-flow: row wrap;
}
.body-li {
  width: 32.8%;
  height: 90px;
  flex: 0 1 auto;
  padding: 8px;
  box-sizing: border-box;
  margin: 1px;
  background: #f5f7fa;
  border-radius: 5px;
}
.li-image {
  height: 58px;
  padding: 4px 0px;
  text-align: center;
  box-sizing: border-box;
}
.custom-image {
  height: 30px;
  width: 30px;
  margin: 0 auto;
}
.li-title {
  line-height: 13px;
  text-align: center;
  font-size: 13px;
}
</style>
